package org.hughie.springframework.beans.factory.annotation;

import cn.hutool.core.bean.BeanUtil;
import org.hughie.springframework.beans.BeansException;
import org.hughie.springframework.beans.PropertyValues;
import org.hughie.springframework.beans.factory.BeanFactory;
import org.hughie.springframework.beans.factory.BeanFactoryAware;
import org.hughie.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.hughie.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.hughie.springframework.utils.ClassUtils;

import java.lang.reflect.Field;

/**
 * 处理 @Value、@Autowired，注解的 BeanPostProcessor
 *
 * @author hughie.cheng
 * @since 2023/9/14
 */
public class AutowiredAnnotationBeanPostProcessor implements
        InstantiationAwareBeanPostProcessor, BeanFactoryAware {

    private DefaultListableBeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = (DefaultListableBeanFactory) beanFactory;
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues propertyValues, Object bean, String beanName) {
        //处理@Value注解
        Class<?> clazz = bean.getClass();
        clazz = ClassUtils.isCglibProxyClass(clazz) ? clazz.getSuperclass() : clazz;
        Field[] declaredFields = clazz.getDeclaredFields();

        for (Field field : declaredFields) {
            Value valueAnnotation = field.getAnnotation(Value.class);
            if (null != valueAnnotation) {
                String value = valueAnnotation.value();
                //根据@Value中的配置 获得对应配置的值
                value = beanFactory.resolveEmbeddedValue(value);
                //通过反射设置属性的值
                BeanUtil.setFieldValue(bean, field.getName(), value);
            }

            Autowired autowired = field.getAnnotation(Autowired.class);
            if (null != autowired) {
                Class<?> fieldType = field.getType();
                String dependentBeanName = null;
                Qualifier qualifier = field.getAnnotation(Qualifier.class);
                Object dependentBean = null;
                if (null != qualifier) {
                    dependentBeanName = qualifier.value();
                    dependentBean = beanFactory.getBean(dependentBeanName, fieldType);
                } else {
                    dependentBean = beanFactory.getBean(fieldType);
                }
                BeanUtil.setFieldValue(bean, field.getName(), dependentBean);
            }
        }
        return propertyValues;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> clazz, String beanName) throws BeansException {
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }
}
